package com.helloae.chap09;

import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFrame;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.datasourcesGDB.ScratchWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeatureClassProxy;
import com.esri.arcgis.geodatabase.ISelectionSet;
import com.esri.arcgis.geodatabase.ITable;
import com.esri.arcgis.geodatabase.ITableProxy;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.geodatabase.TableName;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.geodatabase.esriSelectionOption;
import com.esri.arcgis.geodatabase.esriSelectionType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 通过对dbf表进行查询，来高亮其对应的要素<br>
 * 示例章节：9.3.2
 * @author zhushijie
 */
public class SelectRecords {
	public FeatureLayer selectRecords() throws UnknownHostException, IOException{
		//打开表文件
		WorkspaceName pWSName = new WorkspaceName();
		pWSName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		pWSName.setPathName(ecliseWorkspace + "HelloAE" + File.separator
				+ "DataSet" + File.separator + "chap09");
		TableName pDatasetName = new TableName();
		pDatasetName.setName("idcities.dbf");
		pDatasetName.setWorkspaceNameByRef(pWSName);
		ITable pTable = new ITableProxy(pDatasetName.open());
		
		//查询table
		QueryFilter pQFilter = new QueryFilter();
		pQFilter.setWhereClause("Population > 10000");
		ScratchWorkspaceFactory pScratchWSFactory = new ScratchWorkspaceFactory();
		IWorkspace pScratchWS = pScratchWSFactory.getDefaultScratchWorkspace();
		ISelectionSet pSelectionSet = pTable.select(pQFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption .esriSelectionOptionNormal, pScratchWS);
		
		//将table的查询结果赋给layer
		FeatureLayer pFeatureLayer = new FeatureLayer();
		pFeatureLayer.setFeatureClassByRef(new IFeatureClassProxy(pTable));
		pFeatureLayer.setSelectionSetByRef(pSelectionSet);
		
		//返回layer
		return pFeatureLayer;
	}

	public static void main(String[] args) throws UnknownHostException, IOException {
        EngineInitializer.initializeVisualBeans();
        AoInitialize ao = new AoInitialize();
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
        
        MapBean map = new MapBean();
        
        SelectRecords obj = new SelectRecords();
        
        map.addLayer(obj.selectRecords(), 0);
        
        JFrame frame = new JFrame("Hello Arcobjects");
        frame.add(map, BorderLayout.CENTER);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
	}
}
